﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace HML.Design
{
    /// <summary>
    /// 自定义颜色面板控件
    /// </summary>
    [ToolboxItem(false)]
    public class AlphaColorEditorCustomColorControl : Control
    {
        #region 新增事件

        private static readonly object EventItemClick = new object();
        /// <summary>
        /// 选项单击事件
        /// </summary>
        public event EventHandler ItemClick
        {
            add { Events.AddHandler(EventItemClick, value); }
            remove { Events.RemoveHandler(EventItemClick, value); }
        }

        #endregion

        #region 属性

        /// <summary>
        /// 已选中颜色选项索引
        /// </summary>
        public object SelectedItem
        {
            get
            {
                if (this.selectedIndex < 0 || this.selectedIndex > AlphaColorEditorCustomColorControl.CustomColorValues.Count)
                {
                    return null;
                }
                return AlphaColorEditorCustomColorControl.CustomColorValues[this.selectedIndex].Color;
            }
            set
            {
                if (value == null)
                {
                    if (this.selectedIndex != -1)
                    {
                        this.selectedIndex = -1;
                        this.Invalidate();
                    }
                }
                else
                {
                    Color tmp = (Color)value;
                    int index = -1;
                    for (int i = 0; i < AlphaColorEditorCustomColorControl.CustomColorValues.Count; i++)
                    {
                        if (AlphaColorEditorCustomColorControl.CustomColorValues[i].Color == tmp)
                        {
                            index = i;
                            break;
                        }
                    }
                    if (this.selectedIndex != index)
                    {
                        this.selectedIndex = index;
                        this.Invalidate();
                    }
                }

            }
        }

        private static List<AlphaColorEditorCustomColorItem> customColorValues = null;
        /// <summary>
        /// 自定义颜色
        /// </summary>
        public static List<AlphaColorEditorCustomColorItem> CustomColorValues
        {
            get
            {
                if (AlphaColorEditorCustomColorControl.customColorValues == null)
                {
                    AlphaColorEditorCustomColorControl.customColorValues = new List<AlphaColorEditorCustomColorItem>() {
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,255,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,192,192) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,224,192) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,255,192) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(192,255,192) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(192,255,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(192,192,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,192,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(224,224,224) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,128,128) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,192,128) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,255,128) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(128,255,128) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(128,255,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(128,128,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,128,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(192,192,192) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,0,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,128,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,255,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,255,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,255,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,0,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(255,0,255) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(128,128,128) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(192,0,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(192,64,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(192,192,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,192,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,192,192) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,0,192) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(192,0,192) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(64,64,64) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(128,0,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(128,64,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(128,128,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,128,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,128,128) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,0,128) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(128,0,128) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,0,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(64,0,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(128,64,64) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(64,64,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,64,0) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,64,64) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(0,0,64) },
          new AlphaColorEditorCustomColorItem() { Color=Color.FromArgb(64,0,64) },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White },
          new AlphaColorEditorCustomColorItem() { Color=Color.White }
        };
                }
                return AlphaColorEditorCustomColorControl.customColorValues;
            }
        }

        #endregion

        #region 字段

        /// <summary>
        /// 已选中颜色选项索引
        /// </summary>
        private int selectedIndex = -1;
        /// <summary>
        /// 获取焦点颜色选项索引
        /// </summary>
        private int focusIndex = 0;

        /// <summary>
        /// 当前鼠标已按下的对象索引
        /// </summary>
        private int mousedownobjectindex = -1;

        #endregion

        public AlphaColorEditorCustomColorControl()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.Selectable, true);

            this.InitializeElement();
        }

        #region 重写

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;

            SolidBrush item_sb = new SolidBrush(Color.White);
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    int index = i * 8 + j;
                    item_sb.Color = AlphaColorEditorCustomColorControl.CustomColorValues[index].Color;

                    //颜色块
                    g.FillRectangle(item_sb, AlphaColorEditorCustomColorControl.CustomColorValues[index].Rect);
                    //颜色块边框
                    ControlPaint.DrawBorder(g, AlphaColorEditorCustomColorControl.CustomColorValues[index].Rect, SystemColors.Control, 2, ButtonBorderStyle.Inset, SystemColors.Control, 2, ButtonBorderStyle.Inset, SystemColors.Control, 2, ButtonBorderStyle.Inset, SystemColors.Control, 2, ButtonBorderStyle.Inset);

                    //已选中颜色块
                    if (this.selectedIndex == index)
                    {
                        ControlPaint.DrawBorder(g, Rectangle.Inflate(AlphaColorEditorCustomColorControl.CustomColorValues[index].Rect, 2, 2), SystemColors.ControlText, ButtonBorderStyle.Solid);
                    }

                    //获取焦点颜色块
                    if (this.focusIndex == index)
                    {
                        ControlPaint.DrawFocusRectangle(g, Rectangle.Inflate(AlphaColorEditorCustomColorControl.CustomColorValues[index].Rect, 3, 3), SystemColors.ControlText, SystemColors.Control);
                    }
                }
            }
            item_sb.Dispose();
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);

            this.Invalidate();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);

            this.mousedownobjectindex = -1;
            this.Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (e.Button == MouseButtons.Left)
            {
                if (!this.Focused)
                {
                    this.Focus();
                }

                for (int i = 0; i < AlphaColorEditorCustomColorControl.CustomColorValues.Count; i++)
                {
                    if (AlphaColorEditorCustomColorControl.CustomColorValues[i].Rect.Contains(e.Location))
                    {
                        mousedownobjectindex = i;
                        break;
                    }
                }
            }

        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (e.Button == MouseButtons.Left)
            {
                for (int i = 0; i < AlphaColorEditorCustomColorControl.CustomColorValues.Count; i++)
                {
                    if (AlphaColorEditorCustomColorControl.CustomColorValues[i].Rect.Contains(e.Location))
                    {
                        if (mousedownobjectindex == i)
                        {
                            this.selectedIndex = this.mousedownobjectindex;
                            this.Invalidate();
                        }
                        else
                        {
                            this.mousedownobjectindex = -1;
                        }

                        this.OnItemClick(EventArgs.Empty);

                        break;
                    }
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                for (int i = 0; i < AlphaColorEditorCustomColorControl.CustomColorValues.Count; i++)
                {
                    if (AlphaColorEditorCustomColorControl.CustomColorValues[i].Rect.Contains(e.Location))
                    {
                        if (i >= 48)
                        {
                            AlphaColorEditorCustomColorDialogForm editForm = new AlphaColorEditorCustomColorDialogForm();
                            if (editForm.ShowDialog(this) == DialogResult.Yes)
                            {
                                AlphaColorEditorCustomColorControl.CustomColorValues[i].Color = editForm.alphaColorEditorCustomColorDialog.ColorValue;
                                this.Invalidate();
                            }
                        }
                        break;
                    }
                }

            }

        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Left:
                case Keys.Right:
                case Keys.Up:
                case Keys.Down:
                case Keys.Enter:
                    {
                        return false;
                    }
            }

            return base.ProcessDialogKey(keyData);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            switch (e.KeyCode)
            {
                case Keys.Enter:
                    {
                        this.selectedIndex = this.focusIndex;
                        this.Invalidate();

                        this.OnItemClick(EventArgs.Empty);

                        break;
                    }
                case Keys.Left:
                    {
                        this.UpdateCustomColorItemFocus(-1);
                        break;
                    }
                case Keys.Up:
                    {
                        this.UpdateCustomColorItemFocus(-8);
                        break;
                    }
                case Keys.Right:
                    {
                        this.UpdateCustomColorItemFocus(1);
                        break;
                    }
                case Keys.Down:
                    {
                        this.UpdateCustomColorItemFocus(8);
                        break;
                    }
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            this.InitializeElement();
            this.Invalidate();
        }

        #endregion

        #region 虚方法

        protected virtual void OnItemClick(EventArgs e)
        {
            EventHandler handler = Events[EventItemClick] as EventHandler;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion

        #region 公开方法

        /// <summary>
        /// 初始化控件内部元素布局
        /// </summary>
        public void InitializeElement()
        {
            int item_width = (int)(this.ClientRectangle.Width / (8 + 9 / 4f)); //8 * item_width + 9 * item_width_interval=this.ClientRectangle.Width
            int item_height = item_width;
            int item_width_interval = (int)(item_width / 4f);
            int item_height_interval = (int)(item_width / 4f);
            Size size = new Size(8 * item_width + 9 * item_width_interval, 8 * item_height + 9 * item_height_interval);
            Rectangle rect = new Rectangle(Math.Max(0, (this.ClientRectangle.Width - size.Width) / 2), 0, size.Width, size.Height);

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    AlphaColorEditorCustomColorControl.CustomColorValues[i * 8 + j].Rect = new Rectangle(rect.X + (j + 1) * item_width_interval + j * item_width, rect.Y + (i + 1) * item_height_interval + i * item_height, item_width, item_height); ;
                }
            }
        }

        #endregion 

        #region 私有方法

        /// <summary>
        /// 设置自定义颜色面板鼠标焦点
        /// </summary>
        /// <param name="offset">焦点偏移量</param>
        private void UpdateCustomColorItemFocus(int offset)
        {
            int offset_tmp = this.focusIndex + offset;
            if (offset_tmp < 0)
            {
                offset_tmp += 64;
            }
            if (offset_tmp >= 64)
            {
                offset_tmp -= 64;
            }
            if (this.focusIndex != offset_tmp)
            {
                this.focusIndex = offset_tmp;
                if (this.Focused)
                {
                    this.Invalidate();
                }
            }
        }

        #endregion

        #region 类

        /// <summary>
        /// 自定义颜色选项
        /// </summary>
        public class AlphaColorEditorCustomColorItem
        {
            /// <summary>
            /// 颜色
            /// </summary>
            public Color Color { get; set; }
            /// <summary>
            /// rect
            /// </summary>
            public Rectangle Rect { get; set; }
        }

        #endregion

    }

}
